<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* CRM in Report */
class Crm extends Controller {
	
	function Crm(){
		if(!$this->session->userdata('user_id')){
			redirect($this->config->item('auth'));
		}
        if(!$this->session->userdata('user_group')==$this->config->item('admin')){
			redirect($this->config->item('dashboard'));
		}
        
		$this->load->model('reports');
        $this->load->model('charting');
        //$this->load->library('COM_excel');
		ini_set('memory_limit', '128M'); 
	}
	
	function index(){
	    if($this->session->userdata('user_group')!= $this->config->item('admin')){
            redirect($this->config->item('setting'));
        }
		$data = array(
						'title'		=>	$this->lang->line('report'),
						'form'      =>  array(
                                                'action'            =>  $this->config->item('cust_excel'),
                                                'action2'           =>  $this->config->item('call_excel'),
                                                'action3'           =>  $this->config->item('day_excel'),
                                                'action4'           =>  $this->config->item('week_excel'),
                                                'action5'           =>  $this->config->item('month_excel'),
                                                'action6'           =>  $this->config->item('job_excel'),
                                                'action7'           =>  $this->config->item('fail_excel')
                                             ), 
						'table'		=>	array(
												'sectors'			=>	$this->reports->getSectors(),
												'groups'			=>	$this->reports->getGroups(),
												'sources'			=>	$this->reports->getSources(),
                                                'users'		        =>	$this->reports->getUsers(),
												'status'			=>	$this->reports->getStatus()
											 ),
                        'content'	=> 	'list',
						'menu' 		=> 	array(
												'dashboard_active'	=>	$this->config->item('empty'),
												'contact_active'	=>	$this->config->item('empty'),
                                                'contactdna_active'	=>	$this->config->item('empty'),
												'activity_active'	=>	$this->config->item('empty'),
												'job_active'		=>	$this->config->item('empty'),
												'call_active'		=>	$this->config->item('empty'),
												'user_active'		=>	$this->config->item('empty'),
												'report_active'		=>	$this->config->item('active'),
												'master_active'		=>	$this->config->item('empty'),
												'graphic_active'	=>	$this->config->item('empty'),
												'setting_active'	=>	$this->config->item('empty'),
											 ),
						'tab'		=>	array(
												'tab1'				=>	$this->lang->line('list'),
												'tab1_link'			=>	$this->config->item('list'),
												'tab1_active'		=>	$this->config->item('active'),
                                                'tab2'				=>	'',
												'tab2_link'			=>	$this->config->item('useradd'),
												'tab2_active'		=>	$this->config->item('empty'),
                                                'tab3'				=>	'',
												'tab4'				=>	'',
												'tab5'				=>	'',
												'tab6'				=>	'',
												'tab7'				=>	'',
												'tab8'				=>	''
											 )				
					 );
		$this->load->vars($data);
		$this->load->theme('default');
	}
	
	function customers(){
	    //set_time_limit(1333360);
		ini_set("memory_limit","512M");
		$objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("title")
                    ->setDescription("description");
		$objPHPExcel->setActiveSheetIndex(0);
        
        $styleArray = array( 'borders' => array( 'allborders' => array(
                             'style' => Style_Border::BORDER_THIN )));
        $fill = array(
      			'type'       => Style_Fill::FILL_SOLID,
      			'rotation'   => 0,
      			'startcolor' => array(
      				'rgb' => 'CCCCCC'
      			),
      			'endcolor'   => array(
      				'argb' => 'CCCCCC'
      			)
      		);                     
		/*Data */
		$row=1;
		$col=0;
		
		/* Cust No */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$row,'No');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col)->setWidth(5);
        
		/* Cust Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+1,$row,'Customer Name');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+1)->setWidth(35);
        
		/* PIC Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+2,$row,'PIC');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+2)->setWidth(25);
        
		/* Contact Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+3,$row,'Contact');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+3)->setWidth(15);
        
		/* Fax Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+4,$row,'Fax');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+4)->setWidth(15);
        
		/* Sector Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+5,$row,'Sector');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+5)->setWidth(15);
        
		/* Group Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+6,$row,'Group');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+6)->setWidth(15);
        
		/* Source Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+7,$row,'Source');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+7)->setWidth(15);
        
		/* Status Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+8,$row,'Status');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+8)->setWidth(15);
        
		/* PIC Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+9,$row,'PIC');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+9)->setWidth(15);
        
		$this->db->join('customer_sector cs','cs.id=c.sector_id','LEFT');
		$this->db->join('customer_group cg','cg.id=c.group_id','LEFT');
		$this->db->join('customer_source cso','cso.id=c.source_id','LEFT');
		$this->db->join('customer_status cst','cst.id=c.status_id','LEFT');
        
		if($this->input->post('sector_search')):
            $this->db->where('c.sector_id',$this->input->post('sector_search'));
        endif;
        
        if($this->input->post('group_search')<>""):
            $this->db->where('c.group_id',$this->input->post('group_search'));
        endif;
        
        if($this->input->post('source_search')):
            $this->db->where('c.source_id',$this->input->post('source_search'));
        endif;
        
        if($this->input->post('status_search')):
            $this->db->where('c.status_id',$this->input->post('status_search'));
        endif;
        
        if($this->input->post('rev_search')):
            $this->db->where('c.rev',$this->input->post('rev_search'));
        endif;
		
		$Q=$this->db->get('customers c');
		
        
        
        $rows=$Q->result_array();
        $i=1;
        
        $row=$row+1;
        $col=0;
        foreach($rows as $rec):
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$row,$i);
            //$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+1,$row,$rec['customer_name']);
            //$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+2,$row,$rec['pic']);
            //$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+3,$row,$rec['tlp']);
            //$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+3,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+4,$row,$rec['fax']);
            //$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+4,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+5,$row,$rec['sector_name']);
            //$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+5,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+6,$row,$rec['group_name']);
            //$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+6,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+7,$row,$rec['source_name']);
            //$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+7,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+8,$row,$rec['status_name']);
            //$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+8,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+9,$row,$rec['rev']);
		    //$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+9,$row)->applyFromArray($styleArray);

            $i++;
            $row=$row+1;  
        endforeach;
		
		// Save it as an excel 2003 file
		$objWriter = IOFactory::createWriter($objPHPExcel, "Excel5");
		$file="customer_list.xls";
		$objWriter->save('crm-media/data/customers/'.$file);
		redirect('crm-media/data/customers/'.$file,301);	
	}
    
    function calls(){
	    set_time_limit(3360);   
		$objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("title")
                    ->setDescription("description");
		$objPHPExcel->setActiveSheetIndex(0);
        
        $styleArray = array( 'borders' => array( 'allborders' => array(
                             'style' => Style_Border::BORDER_THIN )));
        $fill = array(
      			'type'       => Style_Fill::FILL_SOLID,
      			'rotation'   => 0,
      			'startcolor' => array(
      				'rgb' => 'CCCCCC'
      			),
      			'endcolor'   => array(
      				'argb' => 'CCCCCC'
      			)
      		);                     
		/*Data */
		$row=1;
		$col=0;
		
		/* Cust No */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$row,'No');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col)->setWidth(5);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,$row)->getFill()->applyFromArray($fill);
		
		/* Cust Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+1,$row,'Customer Name');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+1)->setWidth(35);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row)->applyFromArray($styleArray);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row)->getFill()->applyFromArray($fill);
		
		/* PIC Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+2,$row,'Contact Person');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+2)->setWidth(25);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row)->getFill()->applyFromArray($fill);
		
        
		/* Contact Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+3,$row,'Contact Number');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+3)->setWidth(15);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+3,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+3,$row)->getFill()->applyFromArray($fill);
		
        
		/* Fax Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+4,$row,'Contact Result');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+4)->setWidth(30);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+4,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+4,$row)->getFill()->applyFromArray($fill);
		
        
		/* Sector Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+5,$row,'PIC');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+5)->setWidth(15);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+5,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+5,$row)->getFill()->applyFromArray($fill);
		
        
		/* Group Name */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+6,$row,'Date');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+6)->setWidth(10);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+6,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+6,$row)->getFill()->applyFromArray($fill);
        
		$this->db->join('users u','u.id=c.user_id','LEFT');
        if(($this->input->post('date_start')<>"") && ($this->input->post('date_finish')<>"")  ):
            $this->db->where('c.date_stamp >=',$this->input->post('date_start'));
            $this->db->where('c.date_stamp <=',$this->input->post('date_finish'));
        endif;
        
        if($this->input->post('rev_search')<>""):
            $this->db->where('u.username',$this->input->post('rev_search'));
        endif;
		
        $Q=$this->db->get('calls c');
		
        $row=$row+1;
        $col=0;
        $rows=$Q->result_array();
        $i=1;
        foreach($rows as $rec):
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$row,$i);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+1,$row,$rec['call_name']);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+2,$row,$rec['contact_name']);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+3,$row,$rec['contact_number']);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+3,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+4,$row,$rec['contact_result']);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+4,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+5,$row,$rec['username']);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+5,$row)->applyFromArray($styleArray);
            
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+6,$row,$rec['date_stamp']);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+6,$row)->applyFromArray($styleArray);
            
            $i++;
            $row=$row+1;  
        endforeach;
		
		// Save it as an excel 2003 file
		$objWriter = IOFactory::createWriter($objPHPExcel, "Excel5");
		$file="call_list.xls";
		$objWriter->save('crm-media/data/calls/'.$file);
		redirect('crm-media/data/calls/'.$file,301);	
	}
    
    function actvweekreport(){
        set_time_limit(3360);   
		$objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("title")
                    ->setDescription("description");
		$objPHPExcel->setActiveSheetIndex(0);
        
        $styleArray = array( 'borders' => array( 'allborders' => array(
                             'style' => Style_Border::BORDER_THIN )));
        $fill = array(
      			'type'       => Style_Fill::FILL_SOLID,
      			'rotation'   => 0,
      			'startcolor' => array(
      				'rgb' => 'CCCCCC'
      			),
      			'endcolor'   => array(
      				'argb' => 'CCCCCC'
      			)
      		);   
        
        
        
        if($this->input->post('user')<>""):
            $this->db->where('username',$this->input->post('user'));
        endif;
        $this->db->where('sales','1');
        $this->db->where('enabled','1');
        $this->db->where('group_id',$this->config->item('user'));
        $this->db->where('pos_id',$this->config->item('ta'));
        $u=$this->db->get('users');
        $u=$u->result_array();
        
        $row=1;
        $col=1;
        
        foreach($u as $u):
        /* No */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+1,$row,'Username');
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row)->getFill()->applyFromArray($fill);

		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+2,$row,$u['username']);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row)->applyFromArray($styleArray);
		/* No */
        
        /* Name*/
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+1,$row+1,'Name');
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row+1)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row+1)->getFill()->applyFromArray($fill);

		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+2,$row+1,$u['name']);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row+1)->applyFromArray($styleArray);
		/* Namwe */
        
        /* Periode*/
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+1,$row+2,'Periode');
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row+2)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row+2)->getFill()->applyFromArray($fill);
        
        
		if($this->input->post('periode')==1):
            $periode=$this->lang->line('this_week');
        elseif($this->input->post('periode')==2):
            $periode=$this->lang->line('next_week');
        elseif($this->input->post('periode')==3):
            $periode='This Month Activity';        
        endif;
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+2,$row+2,$periode);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row+2)->applyFromArray($styleArray);
		
        
        /* Header */
        $col=0;
        $row=$row+4;
        /* No */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$row,'No');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col)->setWidth(5);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,$row)->getFill()->applyFromArray($fill);
		/* No */
        
        /* Date */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+1,$row,'Date');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+1)->setWidth(10);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row)->getFill()->applyFromArray($fill);
		/* Date */
        
        /* Customer */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+2,$row,'Customer');
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+2)->setWidth(30);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row)->getFill()->applyFromArray($fill);
		/* Customer */
        
        $this->db->orderby('id','ASC');
        $qa=$this->db->get('activity');
        $qa=$qa->result_array();
        
        $x=$col+3;
        foreach($qa as $qq):
            /* Activity */
		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+$x,$row,$qq['activity_type']);
		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+$x)->setWidth(15);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+$x,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+$x,$row)->getFill()->applyFromArray($fill);
		/* actigyt */
        $x++;
        endforeach;
        
        /* Header */                        
		$next_week=date('W')+1;
        /*Data */
		$row=$row+1;
		$col=0;
        $sql = "  SELECT *,";
		$sql.= " (SELECT COUNT(at.activity_id) FROM activity_transaction at2 WHERE at2.pic=at.pic AND at2.customer_id=at.customer_id AND at2.activity_id=1) AS call1, ";
		$sql.= " (SELECT COUNT(at.activity_id) FROM activity_transaction at2 WHERE at2.pic=at.pic AND at2.customer_id=c.id AND at2.activity_id=2) AS call_r, ";
		$sql.= " (SELECT COUNT(at.activity_id) FROM activity_transaction at2 WHERE at2.pic=at.pic AND at2.customer_id=c.id AND at2.activity_id=3) AS presentation, ";
		$sql.= " (SELECT COUNT(at.activity_id) FROM activity_transaction at2 WHERE at2.pic=at.pic AND at2.customer_id=c.id AND at2.activity_id=4) AS meeting, ";
		$sql.= " (SELECT COUNT(at.activity_id) FROM activity_transaction at2 WHERE at2.pic=at.pic AND at2.customer_id=c.id AND at2.activity_id=5) AS dc ";
		$sql.= " FROM ";
        $sql.= " customers c";
		$sql.= " LEFT JOIN activity_transaction at ON at.customer_id=c.id ";
		$sql.= " WHERE at.pic='".$u['username']."'";
        if($this->input->post('periode')==1):
            $sql.= " AND WEEK(at.activity_date)='".date('W')."'";
        elseif($this->input->post('periode')==2):
            $sql.= " AND WEEK(at.activity_date)='".$next_week."'";
        elseif($this->input->post('periode')==3):
            $sql.= " AND MONTH(at.activity_date)='".date('m')."'"; 
            $periode='This Month Activity';        
        endif;
		
		$sql.= " AND YEAR(at.activity_date)='".date('Y')."'";
		$sql.= " GROUP BY at.customer_id ";
		$q=$this->db->query($sql);
		$rows=$q->result_array();
        
        $i=1;
        foreach($rows as $rec):
		/* No */
    		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$row,$i);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,$row)->applyFromArray($styleArray);
		/* No */
        
        /* Date */
    		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+1,$row,$rec['activity_date']);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row)->applyFromArray($styleArray);
		/* Date */
        
        /* Customer */
    		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+2,$row,$rec['customer_name']);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row)->applyFromArray($styleArray);
		/* Customer */
		
		/* Call */
    		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+3,$row,$rec['call1']);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+3,$row)->applyFromArray($styleArray);
		/* Call */
        
		/* Call */
    		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+4,$row,$rec['call_r']);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+4,$row)->applyFromArray($styleArray);
		/* Call */
		
		/* Call */
    		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+5,$row,$rec['presentation']);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+5,$row)->applyFromArray($styleArray);
		/* Call */
		
		/* Call */
    		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+6,$row,$rec['meeting']);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+6,$row)->applyFromArray($styleArray);
		/* Call */
		
		/* Call */
    		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+7,$row,$rec['dc']);
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+7,$row)->applyFromArray($styleArray);
		/* Call */
        
        $i++;
        $row=$row+1;
        endforeach;
        $i++;
        $row=$row+1;
        endforeach;
        // Save it as an excel 2003 file
		$objWriter = IOFactory::createWriter($objPHPExcel, "Excel5");
        $file="activityWEEKLY.xls";
		$file=$file;
		$path='crm-media/data/activity/week/';
        $objWriter->save($path.$file);
		redirect($path.$file,301);	
    }
    
    function exceljob(){ 
            if($this->input->post('type')=='01'):
                $this->exceldealmonth();
                echo('sukses');
            elseif($this->input->post('type')=='02'):
                $this->excelrealmonth();
                echo('sukses');
            elseif($this->input->post('type')=='03'):
                $this->exceltypemonth();
                echo('sukses'); 
            elseif($this->input->post('type')=='04'):
                $this->excelincomingmonth();
                echo('sukses');
            elseif($this->input->post('type')=='05'):
                $this->excelproductmonth();
                echo('sukses');
            elseif($this->input->post('type')=='06'):
                $this->excelsectormonth();   
            endif;
    }
    
    function exceldealmonth(){
        $excel = new COM_excel;
        $month=$this->input->post('month');
        $year =$this->input->post('year');
        
        /* Sheet 3 */
        $excel->newSheet('1 Year');
    
        $i=0;
        $j=0;
        $final3[$i][0]   =   "";
        $final3[$i+1][0] =   "";
        $final3[$i+2][0] =   month_name('01');
        $final3[$i+3][0] =   month_name('02');
        $final3[$i+4][0] =   month_name('03');
        $final3[$i+5][0] =   month_name('04');
        $final3[$i+6][0] =   month_name('05');
        $final3[$i+7][0] =   month_name('06');
        $final3[$i+8][0] =   month_name('07');
        $final3[$i+9][0] =   month_name('08');
        $final3[$i+10][0]=   month_name('09');
        $final3[$i+11][0]=   month_name('10');
        $final3[$i+12][0]=   month_name('11');
        $final3[$i+13][0]=   month_name('12');
        
        $deal  =  $this->reports->getDealYearly($year); 
        $i=0;
        $j=0;
        foreach($deal as $row):
            $final3[$i+1][$j+1] = $row['username'];
            $final3[$i+2][$j+2] = $row['month_1'];
            $final3[$i+3][$j+3] = $row['month_2'];
            $final3[$i+4][$j+4] = $row['month_3'];
            $final3[$i+5][$j+5] = $row['month_4'];
            $final3[$i+6][$j+6] = $row['month_5'];
            $final3[$i+7][$j+7] = $row['month_6'];
            $final3[$i+8][$j+8] = $row['month_7'];
            $final3[$i+9][$j+9] = $row['month_8'];
            $final3[$i+10][$j+10] = $row['month_9'];
            $final3[$i+11][$j+11] = $row['month_10'];
            $final3[$i+12][$j+12] = $row['month_11'];
            $final3[$i+13][$j+13] = $row['month_12'];
            $j++;
        endforeach;
        $excel->setChartData($final3);
        $excel->setChartType(COM_excel::xlColumnClustered,400,0,900,300);
        $excel->generateGraph("Deal","User List","Total",2,1); 
        /* Sheet 3 */
        
        /* Sheet 2 */
        $excel->newSheet('3 Bulan');
        if(($month=='01') || ($month=='02') || ($month=='03')):
            $month_1  = '01';
            $month_2  = '02';
            $month_3  = '03';
        elseif(($month=='04') || ($month=='05') || ($month=='06')):    
            $month_1  = '04';
            $month_2  = '05';
            $month_3  = '06';
        elseif(($month=='07') || ($month=='08') || ($month=='09')):    
            $month_1  = '07';
            $month_2  = '08';
            $month_3  = '09';
        elseif(($month=='10') || ($month=='11') || ($month=='12')):    
            $month_1  = '10';
            $month_2  = '11';
            $month_3  = '12';         
        endif;
        
        $i=0;
        $j=0;
        $final2[$i][0]   =   "";
        $final2[$i+1][0] =   "";
        $final2[$i+2][0] =   month_name($month_1);
        $final2[$i+3][0] =   month_name($month_2);
        $final2[$i+4][0] =   month_name($month_3);
        $deal  =  $this->reports->getDealMonthly($month_1,$month_2,$month_3,$year); 
        $i=0;
        $j=0;
        foreach($deal as $row):
            $final2[$i+1][$j+1] = $row['username'];
            $final2[$i+2][$j+2] = $row['month_1'];
            $final2[$i+3][$j+3] = $row['month_2'];
            $final2[$i+4][$j+4] = $row['month_3'];
            $j++;
        endforeach;
        $excel->setChartData($final2);
        $excel->setChartType(COM_excel::xlColumnClustered,400,0,600,200);
        $excel->generateGraph("Deal","User List","Total",2,1); 
        /* Sheet 2 */
        
        /* Sheet 1 */
        $excel->newSheet('Bulan '.$month.','.$year);
        $i=0;
        $j=0;
        $final[$i][0]   =   "";
        $final[$i+1][0] =   "";
        $final[$i+2][0] =   "Rev";
        $final[$i+3][0] =   "Target";
        $deal  =  $this->reports->getDeal($month,$year); 
        $i=0;
        $j=0;
        foreach($deal as $row):
            $final[$i+1][$j+1] = $row['username'];
            $final[$i+2][$j+2] = $row['total'];
            $final[$i+3][$j+3] = $row['target'];
            $j++;
        endforeach;
        $excel->setChartData($final);
        $excel->setChartType(COM_excel::xlColumnClustered,400,0,400,200);
        $excel->generateGraph("Deal","User List","Total",2,1); 
        /* Sheet 1 */
        $file='C:\\deal.xls';
        $excel->saveFile($file);
        $file=str_replace($file,site_url(),''); 
        redirect($file);
    }
    
    function excelrealmonth(){
        $excel = new COM_excel;
        $month=$this->input->post('month');
        $year =$this->input->post('year');
        
        /* Sheet 3 */
        $excel->newSheet('1 Year');
    
        $i=0;
        $j=0;
        $final3[$i][0]   =   "";
        $final3[$i+1][0] =   "";
        $final3[$i+2][0] =   month_name('01');
        $final3[$i+3][0] =   month_name('02');
        $final3[$i+4][0] =   month_name('03');
        $final3[$i+5][0] =   month_name('04');
        $final3[$i+6][0] =   month_name('05');
        $final3[$i+7][0] =   month_name('06');
        $final3[$i+8][0] =   month_name('07');
        $final3[$i+9][0] =   month_name('08');
        $final3[$i+10][0]=   month_name('09');
        $final3[$i+11][0]=   month_name('10');
        $final3[$i+12][0]=   month_name('11');
        $final3[$i+13][0]=   month_name('12');
        
        $deal  =  $this->reports->getRealYearly($year); 
        $i=0;
        $j=0;
        foreach($deal as $row):
            $final3[$i+1][$j+1] = $row['username'];
            $final3[$i+2][$j+2] = $row['month_1'];
            $final3[$i+3][$j+3] = $row['month_2'];
            $final3[$i+4][$j+4] = $row['month_3'];
            $final3[$i+5][$j+5] = $row['month_4'];
            $final3[$i+6][$j+6] = $row['month_5'];
            $final3[$i+7][$j+7] = $row['month_6'];
            $final3[$i+8][$j+8] = $row['month_7'];
            $final3[$i+9][$j+9] = $row['month_8'];
            $final3[$i+10][$j+10] = $row['month_9'];
            $final3[$i+11][$j+11] = $row['month_10'];
            $final3[$i+12][$j+12] = $row['month_11'];
            $final3[$i+13][$j+13] = $row['month_12'];
            $j++;
        endforeach;
        $excel->setChartData($final3);
        $excel->setChartType(COM_excel::xlColumnClustered,400,0,900,300);
        $excel->generateGraph("Realisasi","User List","Total",2,1); 
        /* Sheet 3 */
        
        /* Sheet 2 */
        $excel->newSheet('3 Bulan');
        if(($month=='01') || ($month=='02') || ($month=='03')):
            $month_1  = '01';
            $month_2  = '02';
            $month_3  = '03';
        elseif(($month=='04') || ($month=='05') || ($month=='06')):    
            $month_1  = '04';
            $month_2  = '05';
            $month_3  = '06';
        elseif(($month=='07') || ($month=='08') || ($month=='09')):    
            $month_1  = '07';
            $month_2  = '08';
            $month_3  = '09';
        elseif(($month=='10') || ($month=='11') || ($month=='12')):    
            $month_1  = '10';
            $month_2  = '11';
            $month_3  = '12';         
        endif;
        
        $i=0;
        $j=0;
        $final2[$i][0]   =   "";
        $final2[$i+1][0] =   "";
        $final2[$i+2][0] =   month_name($month_1);
        $final2[$i+3][0] =   month_name($month_2);
        $final2[$i+4][0] =   month_name($month_3);
        $deal  =  $this->reports->getRealMonthly($month_1,$month_2,$month_3,$year); 
        $i=0;
        $j=0;
        foreach($deal as $row):
            $final2[$i+1][$j+1] = $row['username'];
            $final2[$i+2][$j+2] = $row['month_1'];
            $final2[$i+3][$j+3] = $row['month_2'];
            $final2[$i+4][$j+4] = $row['month_3'];
            $j++;
        endforeach;
        $excel->setChartData($final2);
        $excel->setChartType(COM_excel::xlColumnClustered,400,0,600,200);
        $excel->generateGraph("Realisasi","User List","Total",2,1); 
        /* Sheet 2 */
        
        /* Sheet 1 */
        $excel->newSheet('Bulan '.$month.','.$year);
        $i=0;
        $j=0;
        $final[$i][0]   =   "";
        $final[$i+1][0] =   "";
        $final[$i+2][0] =   "Rev";
        $final[$i+3][0] =   "Target";
        $deal  =  $this->reports->getReal($month,$year); 
        $i=0;
        $j=0;
        foreach($deal as $row):
            $final[$i+1][$j+1] = $row['username'];
            $final[$i+2][$j+2] = $row['total'];
            $final[$i+3][$j+3] = $row['target'];
            $j++;
        endforeach;
        $excel->setChartData($final);
        $excel->setChartType(COM_excel::xlColumnClustered,400,0,400,200);
        $excel->generateGraph("Realisasi","User List","Total",2,1); 
        /* Sheet 1 */
        
        
        $excel->saveFile('C:\\Realisasi.xls');
        redirect($this->config->item('report'));
    }
    
    function exceltypemonth(){
        $excel = new COM_excel;
        $month=$this->input->post('month');
        $year =$this->input->post('year');
    
        /* Sheet 1 */
        $excel->newSheet('Bulan '.$month.','.$year);
        $i=0;
        $j=0;
        $final[$i][0]   =   "";
        $final[$i+1][0] =   "";
        $final[$i+1][1] =   "Repeat";
        $final[$i+1][2] =   "First";
        $deal  =  $this->reports->getType($month,$year); 
        $i=1;
        $j=0;
        $repeat=0;
        $first=0;
        foreach($deal as $row):
            $final[$i+1][$j]   = $row['username'];
            $final[$i+1][$j+1] = $row['repeat_'];
            $repeat=$repeat+$row['repeat_'];
            $final[$i+1][$j+2] = $row['first_'];
            $first=$first+$row['first_'];
            $i++;
        endforeach;
        $excel->setChartData($final);
        $excel->setChartType(COM_excel::xlColumnClustered,400,0,400,200);
        $excel->generateGraph("Repeat - First","User List","Total",2,2); 
        /* Sheet 1 */
        
        /* Sheet 2 */
        $excel->newSheet('Total '.$month.','.$year);
        $i=0;
        $j=0;
        $final2[$i][0]   =   "";
        $final2[$i+1][0] =   "";
        $final2[$i+1][1] =   "Repeat";
        $final2[$i+1][2] =   "First";
        //$deal  =  $this->reports->getType($month,$year); 
        $i=1;
        $j=0;
         //foreach($deal as $row):
            $final2[$i+1][$j]   = "Total";
            $final2[$i+1][$j+1] = $repeat;
            $final2[$i+1][$j+2] = $first;
            //$i++;
        //endforeach;
        $excel->setChartData($final2);
        $excel->setChartType(COM_excel::xlPie,400,0,400,200);
        $excel->generateGraph("Repeat-First"); 
        /* Sheet 2 */
      
        $excel->saveFile('C:\\First-Repeat.xls');
        redirect($this->config->item('report'));
    }
    
    function excelincomingmonth(){
        $excel = new COM_excel;
        $month=$this->input->post('month');
        $year =$this->input->post('year');
    
        /* Sheet 1 */
        $excel->newSheet('Bulan '.$month.','.$year);
        $i=0;
        $j=0;
        $final[$i][0]   =   "";
        $final[$i+1][0] =   "";
        $final[$i+1][1] =   "Incoming";
        $final[$i+1][2] =   "Non Incoming";
        $deal  =  $this->reports->getIncoming($month,$year); 
        $i=1;
        $j=0;
        $in=0;
        $non=0;
        foreach($deal as $row):
            $final[$i+1][$j]   = $row['username'];
            $final[$i+1][$j+1] = $row['in_'];
            $in=$in+$row['in_'];
            $final[$i+1][$j+2] = $row['non_'];
            $non=$non+$row['non_'];
            $i++;
        endforeach;
        $excel->setChartData($final);
        $excel->setChartType(COM_excel::xlColumnClustered,400,0,400,200);
        $excel->generateGraph("Incoming - Non","User List","Total",2,2); 
        /* Sheet 1 */
        
        /* Sheet 2 */
        $excel->newSheet('Total '.$month.','.$year);
        $i=0;
        $j=0;
        $final2[$i][0]   =   "";
        $final2[$i+1][0] =   "";
        $final2[$i+1][1] =   "Incoming";
        $final2[$i+1][2] =   "Non Incoming";
        //$deal  =  $this->reports->getType($month,$year); 
        $i=1;
        $j=0;
         //foreach($deal as $row):
            $final2[$i+1][$j]   = "Total";
            $final2[$i+1][$j+1] = $in;
            $final2[$i+1][$j+2] = $non;
            //$i++;
        //endforeach;
        $excel->setChartData($final2);
        $excel->setChartType(COM_excel::xlPie,400,0,400,200);
        $excel->generateGraph("Incoming - Non Incoming"); 
        /* Sheet 2 */
        $excel->saveFile('C:\\Incoming-Non_Incoming.xls');
        redirect($this->config->item('report'));
    }
    
    function excelproductmonth(){
        $excel = new COM_excel;
        $month=$this->input->post('month');
        $year =$this->input->post('year');

        /* Sheet 1 */
        $excel->newSheet('Bulan '.$month.','.$year);
        
        $deal  =  $this->reports->getUserp(); 
        $i=0;
        $j=0;
        $final[$i][0]   =   "";
        $final[$i+1][0] =   "";
        foreach($deal as $row):
            $final[$i+1][$j+1] = $row['username'];
            $j++;
        endforeach;
        
        $i=0;
        $j=0;
        $final[$i][0]   =   "";
        $final[$i+1][0] =   "";
        $prod=$this->reports->getProducts();
        foreach($prod as $rec):
            $final[$i+2][0] =   $rec['product_name'].'-'.$rec['product_time'];
            $user=$this->reports->getUserp(); 
            $j=1;
            foreach($user as $row):
                $jobs = $this->reports->getJobProducts($row['username'],$rec['id'],$month,$year);
                if(count($jobs)>=1):
                    $total=$jobs['total'];
                else:
                    $total=0;
                endif;
                        
                $final[$i+2][$j+1] = $total ;
                $j++;
            endforeach;
            $i++;
        endforeach;
        
        $excel->setChartData($final);
        $excel->setChartType(COM_excel::xlColumnClustered,400,0,400,200);
        $excel->generateGraph("Product List","User List","Total",2,2); 
        /* Sheet 1 */
        
        
        /* Sheet 2 */
        $excel->newSheet('Total '.$month.','.$year);
        $i=0;
        $j=0;
        $final2[$i][0]   =   "";
        $final2[$i+1][0] =   "";
        $prod=$this->reports->getProducts();
        foreach($prod as $rec):
            $final2[$i+2][0] =   $rec['product_name'].'-'.$rec['product_time'];
            $user=$this->reports->getUserp(); 
            $j=1;
            //foreach($user as $row):
                $jobs = $this->reports->getJobProductTotal($rec['id'],$month,$year);
                if(count($jobs)>=1):
                    $total=$jobs['total'];
                else:
                    $total=0;
                endif;      
                $final2[$i+2][$j+1] = $total ;
                //$j++;
            //endforeach;
            $i++;
        endforeach;
        
        $excel->setChartData($final2);
        $excel->setChartType(COM_excel::xlColumnClustered,400,0,400,200);
        $excel->generateGraph("Product List","User List","Total",2,2); 
        /* Sheet 2 */
        
        
        $excel->saveFile('C:\\products.xls');
        redirect($this->config->item('report'));
    }
    
    function excelsectormonth(){
        $excel = new COM_excel;
        $month=$this->input->post('month');
        $year =$this->input->post('year');

        /* Sheet 1 */
        $excel->newSheet('Bulan '.$month.','.$year);
        
        $deal  =  $this->reports->getUserp(); 
        $i=0;
        $j=0;
        $final[$i][0]   =   "";
        $final[$i+1][0] =   "";
        foreach($deal as $row):
            $final[$i+1][$j+1] = $row['username'];
            $j++;
        endforeach;
        
        $i=0;
        $j=0;
        $final[$i][0]   =   "";
        $final[$i+1][0] =   "";
        $sec=$this->reports->getSectors();
        foreach($sec as $rec):
            $final[$i+2][0] =   $rec['sector_name'];
            $user=$this->reports->getUserp(); 
            $j=1;
            foreach($user as $row):
                $jobs = $this->reports->getJobSectors($row['username'],$rec['id'],$month,$year);
                if(count($jobs)>=1):
                    $total=$jobs['total'];
                else:
                    $total=0;
                endif;
                        
                $final[$i+2][$j+1] = $total ;
                $j++;
            endforeach;
            $i++;
        endforeach;
        
        $excel->setChartData($final);
        $excel->setChartType(COM_excel::xlColumnClustered,400,0,400,200);
        $excel->generateGraph("Sector List","User List","Total",2,2); 
        /* Sheet 1 */
        
        
        /* Sheet 2 */
        $excel->newSheet('Total '.$month.','.$year);
        $i=0;
        $j=0;
        $final2[$i][0]   =   "";
        $final2[$i+1][0] =   "";
        $sec=$this->reports->getSectors();
        foreach($sec as $rec):
            $final2[$i+2][0] =   $rec['sector_name'];
            //$user=$this->reports->getUserp(); 
            $j=1;
            //foreach($user as $row):
                $jobs = $this->reports->getJobSectorTotal($rec['id'],$month,$year);
                if(count($jobs)>=1):
                    $total=$jobs['total'];
                else:
                    $total=0;
                endif;
                        
                $final2[$i+2][$j+1] = $total ;
                //$j++;
            //endforeach;
            $i++;
        endforeach;
        
        $excel->setChartData($final2);
        $excel->setChartType(COM_excel::xlColumnClustered,400,0,400,200);
        $excel->generateGraph("Sector List","User List","Total",2,2); 
        /* Sheet 2 */
        
        
        $excel->saveFile('C:\\sector.xls');
        redirect($this->config->item('report'));
    }
    
    function salesreport(){
        $file_type="xls";
        $file_name="sales_report.xls";
        header("Content-Type: application/$file_type");
        header("Content-Disposition: attachment; filename=$file_name");
        header("Pragma: no-cache");
        header("Expires: 0"); 
        set_time_limit(3360);   
		$objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("title")
                    ->setDescription("description");
		$objPHPExcel->setActiveSheetIndex(0);
        
        $styleArray = array( 'borders' => array( 'allborders' => array(
                             'style' => Style_Border::BORDER_THIN )));
        $fill = array(
      			'type'       => Style_Fill::FILL_SOLID,
      			'rotation'   => 0,
      			'startcolor' => array(
      				'rgb' => 'CCCCCC'
      			),
      			'endcolor'   => array(
      				'argb' => 'CCCCCC'
      			)
      		);   
        
        $row=1;
        $col=0;
        
        $this->db->where('username',$this->input->post('username'));
        $u=$this->db->get('users');
        $u=$u->row_array();
      
        /* Produk */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$row,'Produk');
        $objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col)->setWidth(20);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,$row)->getFill()->applyFromArray($fill);
        /* Produk */
        
        /* Tanggal Awal */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+1,$row,'Tanggal Awal');
        $objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+1)->setWidth(15);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+1,$row)->getFill()->applyFromArray($fill);
        /* Tanggal Awal */
          
        /* Tanggal Akhir */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+2,$row,'Tanggal Akhir');
        $objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+2)->setWidth(15);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row)->getFill()->applyFromArray($fill);
		/* Tanggal Akhir */
        
        /* Perusahaan */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+3,$row,'Perusahaan');
        $objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+3)->setWidth(25);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+3,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+3,$row)->getFill()->applyFromArray($fill);
        /* Perusahaan */
        
        /* Status */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+4,$row,'Status');
        $objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+4)->setWidth(10);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+4,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+4,$row)->getFill()->applyFromArray($fill);
        /* Status */
        
        /* Investasi */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+5,$row,'Investasi');
        $objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+5)->setWidth(15);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+5,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+5,$row)->getFill()->applyFromArray($fill);
        /* Investasi */
        
        /* Note */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+6,$row,'Note');
        $objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($col+6)->setWidth(15);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+6,$row)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+6,$row)->getFill()->applyFromArray($fill);
        /* Note */
        
        
        
        //$this->db->select('*');
        //$this->db->join('customers c','c.id=sf.cust_id','LEFT');
        //$this->db->join('products p','p.id=sf.prod_id','LEFT');
        //$this->db->where('sf.date_in>=',$this->input->post('date_in'));
        //$this->db->where('sf.date_in<=',$this->input->post('date_out'));
        //$q=$this->db->get('sales_fails sf');
		$sql =" SELECT * ";
        $sql.=" FROM sales_fails sf ";
        $sql.=" LEFT JOIN products p ON p.id=sf.prod_id ";
        $sql.=" LEFT JOIN customers c ON c.id=sf.cust_id ";
        $sql.=" WHERE sf.date_in>='".$this->input->post('date_in')."'";
        $sql.=" AND sf.date_in<='".$this->input->post('date_out')."'";
        if($this->input->post('rev_search')):
            $sql.=" AND c.rev = '".$this->input->post('rev_search')."'";
        endif;
        $q=$this->db->query($sql);
        $rows=$q->result_array();
        
        $row=$row+1;
        foreach($rows as $value):    
        /* Produk */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$row,$value['product_name']);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row)->applyFromArray($styleArray);
        /* Produk */
        
        /* Tanggal Awal */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+1,$row,$value['date_in']);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row)->applyFromArray($styleArray);
        /* Tanggal Awal */
          
        /* Tanggal Akhir */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+2,$row,$value['date_out']);
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+2,$row)->applyFromArray($styleArray);
		/* Tanggal Akhir */
        
        /* Perusahaan */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+3,$row,$value['customer_name']);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+3,$row)->applyFromArray($styleArray);
        /* Perusahaan */
        
        /* Status */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+4,$row,$value['status']);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+4,$row)->applyFromArray($styleArray);
        /* Status */
        
        /* Investasi */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+5,$row,$value['invest']);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+5,$row)->applyFromArray($styleArray);
        /* Investasi */
        
        /* Note */
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col+6,$row,$value['note']);
        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col+6,$row)->applyFromArray($styleArray);
        /* Note */
        $row=$row+1;    
        endforeach;
        
        // Save it as an excel 2003 file
		$objWriter = IOFactory::createWriter($objPHPExcel, "Excel5");
		$file=$file_name.'.'.$file_type;
		$path='crm-media/data/activity/week/';
        $objWriter->save($path.$file);
		redirect($path.$file,301);	
    }
}

/* End of file dashboard.php */
/* Location: ./system/application/modules/matchbox/controllers/dashboard.php */